package br.com.gfkauer.converter;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.*;

import br.com.gfkauer.model.entity.Equipment;
import br.com.gfkauer.model.facade.EquipmentFacade;


@FacesConverter(forClass = br.com.gfkauer.model.entity.Equipment.class)
public class EquipmentConverter implements Converter {

   @Override
   public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
       EquipmentFacade equipmentFacade = new EquipmentFacade();
       int equipmentId;

       try {
           equipmentId = Integer.parseInt(arg2);
       } catch (NumberFormatException exception) {
           throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Type the name of a Dog and select it (or use the dropdow)", "Type the name of a Equipment and select it (or use the dropdow)"));
       }

       return equipmentFacade.findEquip(equipmentId);
   }

   @Override
   public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

       if (arg2 == null) {
           return "";
       }
       Equipment equipment = (Equipment) arg2;
       return String.valueOf(equipment.getId());
   }
}